<?php

/**
 * Created by PhpStorm.
 * User: HJJ
 * Date: 2022-03-01
 * Time: 上午10:47:18
 * Info:
 */

namespace app\admin\controller\user;

use app\admin\annotation\ControllerAnnotation;
use app\admin\annotation\NodeAnotation;
use app\common\controller\AdminController;
use app\common\model\User as UserModel;
use think\Exception;
use think\App;

/**
 * @ControllerAnnotation(title="用户管理")
 * Class Node
 * @package app\admin\controller\user
 */
class UserController extends AdminController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new UserModel();
    }

    /**
     * @NodeAnotation(title="用户列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $page  = (int)$this->request->param('page', 1);
            $limit = (int)$this->request->param('limit', 10);
            $first = ($page - 1) * $limit;
            $key   = $this->request->param('key');
            $where = function ($query) use ($key) {
                if (!empty($key['title'])) {
                    $query->whereLike('nickname', '%'.$key['title'].'%');
                    $query->whereLike('mobile', '%'.$key['title'].'%');
                }
                if (isset($key['sex'])) {
                    $query->where('sex', $key['sex']);
                }
            };
            $count = $this->model->where($where)->count();
            $list  = $this->model->where($where)->limit($first, $limit)->order('id desc')->select();
            $data = [
                'code'  => 0,
                'msg'   => 'ok',
                'count' => $count,
                'data'  => $list
            ];

            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加用户")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $rule  = [
                'nickname|昵称' => 'require',
                'mobile|手机号'   => 'require|mobile'
            ];
            $this->validate($param, $rule);
            $userModel = new UserModel();

            $result = $userModel->manageAdd($param);
            if ( ! empty($result['code']) && $result['code'] == 200) {
                $this->success($result['msg']);
            } else {
                $this->error($result['msg']);
            }
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑用户")
     */
    public function edit()
    {
        $userModel = new UserModel();

        if ($this->request->isPost()) {
            $param = $this->request->param();
            $rule  = [
                'nickname|昵称' => 'require',
                'mobile|手机号'   => 'require|mobile'
            ];
            $this->validate($param, $rule);

            $result = $userModel->manageEdit($param);
            if ( !empty($result['code']) && $result['code'] == 200) {
                $this->success($result['msg']);
            } else {
                $this->error($result['msg']);
            }
        }
        $user_id = $this->request->param('id');
        $data    = $userModel->getUserInfo($user_id);
        if (empty($data['data'])) {
            $this->error('获取信息失败');
        }

        $this->assign('data', $data['data']);

        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="删除用户")
     */
    public function delete()
    {
        $user_id   = $this->request->param('id');
        $userModel = new UserModel();
        $result    = $userModel->manageDel($user_id);
        if ( ! empty($result['code']) && $result['code'] == 200) {
            $this->success($result['msg']);
        } else {
            $this->error($result['msg']);
        }
    }

}